#include "battle.h"
#include "fighter.h"
#include "monster.h"
#include "player.h"

// Constructor
BattleClass::BattleClass() {
	Timer = 0;
	LeftFighterCount = 0;
	RightFighterCount = 0;
	PlayerCount = 0;
	MonsterCount = 0;
}

// Destructor
BattleClass::~BattleClass() {

	// Delete monsters
	for(u32 i = 0; i < Fighters.size(); i++) {
		if(Fighters[i] && Fighters[i]->GetType() == FighterClass::TYPE_MONSTER)
			delete Fighters[i];
	}
}

// Add a fighter to the battle
void BattleClass::AddFighter(FighterClass *TFighter, int TSide) {
	
	// Count fighters and set slots
	if(TSide == 0) {
		TFighter->SetSlot(TSide + LeftFighterCount * 2);
		LeftFighterCount++;
	}
	else {
		TFighter->SetSlot(TSide + RightFighterCount * 2);
		RightFighterCount++;
	}

	if(TFighter->GetType() == FighterClass::TYPE_PLAYER)
		PlayerCount++;
	else
		MonsterCount++;

	Fighters.push_back(TFighter);	
}

// Get a list of fighters from a side
void BattleClass::GetFighterList(int TSide, array<FighterClass *> &TFighters) {

	for(u32 i = 0; i < Fighters.size(); i++) {
		if(Fighters[i] && Fighters[i]->GetSide() == TSide) {
			TFighters.push_back(Fighters[i]);
		}
	}
}

// Get a list of alive fighters from a side
void BattleClass::GetAliveFighterList(int TSide, array<FighterClass *> &TFighters) {

	for(u32 i = 0; i < Fighters.size(); i++) {
		if(Fighters[i] && Fighters[i]->GetSide() == TSide && Fighters[i]->GetHealth() > 0) {
			TFighters.push_back(Fighters[i]);
		}
	}
}

// Get a list of monster from the right side
void BattleClass::GetMonsterList(array<MonsterClass *> &TMonsters) {

	for(u32 i = 0; i < Fighters.size(); i++) {
		if(Fighters[i] && Fighters[i]->GetSide() == 1 && Fighters[i]->GetType() == FighterClass::TYPE_MONSTER) {
			TMonsters.push_back(static_cast<MonsterClass *>(Fighters[i]));
		}
	}
}

// Get a list of players from a side
void BattleClass::GetPlayerList(int TSide, array<PlayerClass *> &TPlayers) {

	for(u32 i = 0; i < Fighters.size(); i++) {
		if(Fighters[i] && Fighters[i]->GetSide() == TSide && Fighters[i]->GetType() == FighterClass::TYPE_PLAYER) {
			TPlayers.push_back(static_cast<PlayerClass *>(Fighters[i]));
		}
	}
}

// Gets a fighter index from a slot number
int BattleClass::GetFighterFromSlot(int TSlot) {
	for(u32 i = 0; i < Fighters.size(); i++) {
		if(Fighters[i] && Fighters[i]->GetSlot() == TSlot) {
			return i;
		}
	}

	return -1;
}
